const g = getApp().globalData
const util = g.util

Page({

    data: {
        deviceList: [],
        name: '',
    },

    choosedDevices: [],
    onLoad(options) {
        this.id = options.id
        this.setData({
            name: options.name
        })

        this.getDeviceList()
    },

    //获取用户权限列表
    getDeviceList() {
        let url = `role/getAdminUserPer?id=${this.id}`
        util.httpGet({
            url,
            header: { authorization: g.userInfo.token },
            fn: res => {
                let deviceList = res.data.result
                this.setData({
                    deviceList,
                })
            }
        })
    },

    checkboxChange(e) {
        console.log(e)
        this.choosedDevices = e.detail.value
    },

    //点确定
    confirmTap() {
        let url = `role/upPermission`
        let body = {
            id: this.id,
            permission: this.choosedDevices
        }
        util.showLoading('编辑中...')
        util.httpPost({
            url,
            body,
            header: { authorization: g.userInfo.token },
            fn: res => {
                util.hideLoading()
                wx.navigateBack({
                    delta: 1,
                })
            },
            failFn: failRes => {
                util.hideLoading()
                wx.showToast({
                    title: failRes.data.msg,
                })
            }
        })
    },

    //点击取消
    cancelTap() {
        wx.navigateBack({
            delta: 1,
        })
    },
})